package com.jx.mlxg.controller;

import com.jx.mlxg.entity.Users;
import com.jx.mlxg.service.UsersService;
import com.jx.mlxg.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户相关
 * */
@RestController
@RequestMapping("/user")
public class UsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 获取用户信息
     */
    @GetMapping("/center")
    public ResponseResult<Object> getUserInfo() {
        try {
            // 从 SecurityContextHolder 中获取当前登录用户的信息
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            // 假设 principal 是一个字符串，表示用户的 openId 或 userId
            if (principal instanceof String) {
                String openIdStr = (String) principal;

                // 根据 userId 获取用户信息和订单统计
                Users userInfoAndOrderStatistics = usersService.findByOpenId(openIdStr);
                ArrayList<Object> list = new ArrayList<>();
                HashMap<String, Object> insertMap = new HashMap<>();
                insertMap.put("title","总数");
                insertMap.put("content","999");
                HashMap<String, Object> insertMap2 = new HashMap<>();
                insertMap2.put("title","待发货");
                insertMap2.put("content","999");
                HashMap<String, Object> insertMap3 = new HashMap<>();
                insertMap3.put("title","已发货");
                insertMap3.put("content","999");
                HashMap<String, Object> insertMap4 = new HashMap<>();
                insertMap4.put("title","已完成");
                insertMap4.put("content","999");
                HashMap<String, Object> insertMap5 = new HashMap<>();
                insertMap5.put("userType",userInfoAndOrderStatistics.getRole());
                list.add(insertMap);
                list.add(insertMap2);
                list.add(insertMap3);
                list.add(insertMap4);

                HashMap<String, Object> hashMap = new HashMap<>();
                ArrayList<Object> list1 = new ArrayList<>();
                list1.add("receiverAddress");
                hashMap.put("userName", userInfoAndOrderStatistics.getUsername());
                hashMap.put("hasBoughtCount", 999);
                hashMap.put("statistics", list);
                hashMap.put("commonOperations",list1);
                hashMap.put("userId",userInfoAndOrderStatistics.getOpenId());
                hashMap.put("userType",userInfoAndOrderStatistics.getRole());
                return ResponseResult.success(hashMap);
            }

            return ResponseResult.failure("无法获取用户信息");
        } catch (Exception e) {
            return ResponseResult.failure("获取用户信息失败: " + e.getMessage());
        }
    }

}
